package com.weifly.weistock.record.option.impl;

import com.weifly.weistock.record.option.OptionMonitorService;
import com.weifly.weistock.record.option.OptionStoreService;
import com.weifly.weistock.record.option.OptionUtils;
import com.weifly.weistock.record.option.domain.MergeRecordResult;
import com.weifly.weistock.record.option.domain.OptionRecordDto;
import com.weifly.weistock.record.option.domain.OptionYearDto;

import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 交易记录管理服务实现
 *
 * @author weifly
 * @since 2019/11/18
 */
public class OptionMonitorServiceImpl implements OptionMonitorService {

    private SortedMap<String, OptionYearDto> yearMap = new TreeMap<>(); // key有序的map

    private OptionStoreService optionStoreService;

    public void setOptionStoreService(OptionStoreService optionStoreService) {
        this.optionStoreService = optionStoreService;
    }

    @Override
    public void updateYearList(List<OptionYearDto> yearList) {
        this.yearMap.clear();
        for(OptionYearDto yearDto : yearList){
            this.yearMap.put(yearDto.getYear(), yearDto);
        }
    }

    @Override
    public MergeRecordResult mergeRecordList(List<OptionRecordDto> recordList) {
        MergeRecordResult result = new MergeRecordResult();
        if(recordList==null || recordList.isEmpty()){
            return result;
        }

        Map<String, OptionYearDto> modifyYearMap = new TreeMap<>(); // 被修改的年记录
        result.setTotal(recordList.size());

        // 合并record到month
        for(OptionRecordDto recordDto : recordList){
            OptionUtils.checkEmpty(recordDto.getDate(), "发生日期不能为空");
            OptionUtils.checkEmpty(recordDto.getEntrustCode(), "委托编号不能为空");
            String recordYear = recordDto.getDate().substring(0, 4);
            OptionYearDto yearDto = this.findOrCreateYear(recordYear);
            OptionUtils.addRecordToYear(yearDto, recordDto, result);
            modifyYearMap.put(recordYear, yearDto);
        }

        // 交易记录排序
        for(OptionYearDto yearDto : modifyYearMap.values()){
            yearDto.getRecordList().sort((r1, r2) ->{
                int compareValue = r1.getDate().compareTo(r2.getDate());
                if(compareValue==0){
                    // 日期相同，比较时间
                    compareValue = r1.getTime().compareTo(r2.getTime());
                    if(compareValue==0){
                        // 时间相同，比较委托号
                        compareValue = r1.getEntrustCode().compareTo(r2.getEntrustCode());
                    }
                }
                return compareValue;
            });
        }

        // 写入存储
        for(OptionYearDto yearDto : modifyYearMap.values()){
            this.optionStoreService.saveOptionYear(yearDto);
        }

        return result;
    }

    private OptionYearDto findOrCreateYear(String year){
        OptionYearDto yearDto = this.yearMap.get(year);
        if(yearDto==null){
            yearDto = new OptionYearDto();
            yearDto.setYear(year);
            this.yearMap.put(year, yearDto);
        }
        return yearDto;
    }
}
